home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.5 Applications 2004 May / SGI IRIX 6.5 Applications 2004 May.iso / dev / java2_dev.idb / usr / demos / java2 / JNI-invocation-example / invoke.c.z / invoke.c
C/C++ Source or Header  |  2004-02-24  |  3KB  |  92 lines

  1. /*
  2.   This demo example is based on an example taken from the Sun Java site.
  3.   See http://www.javasoft.com/docs/books/tutorial/native1.1
  4. */
  5.  
  6. #include <jni.h>
  7. #include <malloc.h>
  8. #include <stdlib.h>
  9.  
  10. #include "Prog.h"
  11.  
  12.  
  13. main() {
  14.     JNIEnv *env;
  15.     JavaVM *jvm;
  16.     JDK1_1InitArgs vm_args;
  17.     jint res;
  18.     jclass cls;
  19.     jmethodID mid;
  20.     jstring jstr;
  21.     jobjectArray args;
  22.     char *env_classpath=(char *)0;
  23.     /* char *newclasspath=(char *)0;  unused */
  24.     char *myclass="Prog";
  25.     char *mymethod="main";
  26.     char *mymethodsig="([Ljava/lang/String;)V";
  27.  
  28.  
  29.     /* IMPORTANT: specify vm_args version # if you use JDK1.1.2 and beyond */
  30.     vm_args.version = 0x00010002;
  31.  
  32.     JNI_GetDefaultJavaVMInitArgs(&vm_args);
  33.  
  34.     /* From 1.1.4 on, JNI_GetDefaultJavaVMInitArgs() sets the default
  35.        class path to contain the system classes. */
  36.     printf("\nDefault CLASSPATH is: %s\n\n", vm_args.classpath);
  37.  
  38.     /*
  39.      * It is possible to modify the CLASSPATH any way we like here.
  40.      * Unfortunately, at this point is too late to set LD_LIBRARY_PATH
  41.      * or LD_LIBRARYN32_PATH.  So our script did both jobs for us, and
  42.      * now we simply need to set the VM's class path from the
  43.      * environment.
  44.      */
  45.  
  46.     /* Check to see if the user has CLASSPATH set. */
  47.     env_classpath=getenv("CLASSPATH");
  48.  
  49.     if (env_classpath) {
  50.       vm_args.classpath = env_classpath;
  51.       printf("\nNew CLASSPATH is: %s\n\n", vm_args.classpath);
  52.     } else {
  53.       printf("CLASSPATH is not set -- no user classes will be loaded.\n\n");
  54.     }
  55.  
  56.     res = JNI_CreateJavaVM(&jvm,(void**)&env,&vm_args);
  57.     if (res < 0) {
  58.         fprintf(stderr, "Can't create Java VM\n");
  59.         exit(1);
  60.     }
  61.  
  62.     cls = (*env)->FindClass(env, myclass);
  63.     if (cls == 0) {
  64.         fprintf(stderr, "Can't find class %s.\n",myclass);
  65.         exit(1);
  66.     }
  67.  
  68.     mid = (*env)->GetStaticMethodID(env, cls, mymethod, mymethodsig);
  69.     if (mid == 0) {
  70.         fprintf(stderr, "Can't find method %s.%s().\n",mymethod,myclass);
  71.         exit(1);
  72.     }
  73.  
  74.     jstr = (*env)->NewStringUTF(env, "called from C!");
  75.     if (jstr == 0) {
  76.         fprintf(stderr, "Out of memory.\n");
  77.         exit(1);
  78.     }
  79.     args = (*env)->NewObjectArray(env, 1, 
  80.                         (*env)->FindClass(env, "java/lang/String"), jstr);
  81.     if (args == 0) {
  82.         fprintf(stderr, "Out of memory.\n");
  83.         exit(1);
  84.     }
  85.     printf("Calling Java method %s.%s()...\n" ,mymethod,myclass);
  86.     fflush(stdout);
  87.     (*env)->CallStaticVoidMethod(env, cls, mid, args);
  88.     printf("Returned from Java method %s.%s().\n" ,mymethod,myclass);
  89.  
  90.     (*jvm)->DestroyJavaVM(jvm);
  91. }
  92.